/*
* created by nobles0ul on 27th sept 2011
* copyright (c) nobles0ul all rights reserved

*/

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<string>
#include"date.h"

using namespace std;


int Date::leapyear(int y)
{
	if((y%400 == 0)|| (y%100!=0 && y%4==0))
		return 1;
		else
			return 0;
	
}
int Date::getday()
{
	return day;
}

int Date::getmonth()
{
	return month;
}

int Date::getyear()
{
	return year;
}
Date::Date(int dd, int mm , int yyyy)
{
	setyear(yyyy);
	setmonth(mm);
	setday(dd);

}
Date::Date(Date & d1)
{
	day = d1.getday();
	month = d1.getmonth();
	year = d1.getyear();
}
void Date::setyear(int y)
{
	year = (y>1900 && y<2100) ?y :1900 ;
}

void Date::setmonth(int m)
{
	month = (m>0 && m<13) ? m :1 ;
}

void Date::setday(int d)
{
	int daysofmonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31 } ;
    if(month==2 && leapyear(year))
{
	day =(d>0 && d<30) ? d : 1;
} 
	else
	{
	day = (d > 0 && d<=daysofmonth[month]) ? d :1;
	}


}


int Date::endofMonth(int day)
{
	if(leapyear(year))
	{
		if(day==29)
		{
			return 1;
			
		}
		else
			return 0;
	}
	else
	{
	int enddays[13] = {0,31,28,31,30,31,20,31,31,30,31,30,31};
	if(day == enddays[month])
		return 1;
		else 
			return 0;
}
}
void Date::upDate(void)
{
   if(!endofMonth(day))
   {
	day++;
   }	


else
{
	if(month==12)
	{
		year++;
		month =1;
		day=1;
	}
	
	else
	{
		month++;
		day=1;
	}
}
}

Date & Date::operator ++(void)
{
	upDate();
	return *this;
}

Date & Date::operator +=(int d)
{
	for(int i=0;i<d;i++)
	{
		upDate();
	}
	
}

Date & Date::operator =(Date & d1)
{
	day = d1.getday();
	month = d1.getmonth();
	year = d1.getyear();
	return *this;
}
bool Date::operator ==(Date & d1)
{
	if(year!=d1.getyear())
	{
		return false;
	}
	else if(month != d1.getmonth())
	{
		return false;
	}
	else if(day !=d1.getday())
	{
		return false;
	}
	else
	{
		return 0;
	}
}

ostream &operator<<(ostream & out , Date &d1)
{
	static string monthname[13] = {"" , "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "september" , "October" , "November" , "December"};
	out<<"\n";
	out<<" "<<d1.day<<"-"<<monthname[d1.month]<<"-"<<d1.year;
	out<<"\n";
	return out;
}
istream &operator>>(istream & in , Date & d2)
{
	int dd , mm, yyyy;
	in>>setw(2)>>dd;
	in.ignore(1);
	in>>setw(2)>>mm;
	in.ignore(1);
	in>>yyyy;
	
	d2.setday(dd);
	d2.setmonth(mm);
	d2.setyear(yyyy);
	return in;
}
